<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        $this->display('login');
    }
    public function login()
    {
        $username = $_POST['username'];
        $password =md5($_POST['password']);
        $user=M('user');
        $ls=$user->where("username='$username'&&password='$password'")->find();

        if ( $ls) {
            // $this->success('用户登录成功！');
            //记住用户的登录session
            $_SESSION['username']= $_POST['username'];
            $_SESSION['role']= $ls['role']==1?"班主任":"年级主任";
            $_SESSION['class']=$ls['class'];
            $_SESSION['grade']=$ls['grade'];
            $this->assign('username',  $_SESSION['username']);
            $this->assign('role',$_SESSION['role']);
            //$this->display('showIndex');//display下面跟模板
           // $this->success(U('User/showAll')); //redirect下面跟控制器
            $this->success('登陆成功！！',U('User/showAll'));
        } else {
            $this->error('用户名或密码错误！');
            $this->redirect('index/index'); //redirect下面跟控制器
        }
    }

    function  logout()
    {
        unset($_SESSION['username']);
        //  $this->success('退出成功！！');
        $this->success('登出成功！！',U('Index/index'));
        //$this->redirect('showLogin');//重定向到控制器的方法  display是本控制的方法

    }
}